package com.librarySystem.action;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.Map;

import com.librarySystem.dto.UserDTO;
import com.librarySystem.entity.User;
import com.librarySystem.service.UserServiceInterface;

public class UserAction extends BaseLibrarySystemAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserServiceInterface userService;
	
	private User user;
	private String msg;
	private UserDTO userDTO;
	private String updataMsg;
	
	private Map<String, Object> session;
	
	private InputStream inputStream;
    public InputStream getInputStream() {
        return inputStream;
    }

	
	public String login(){
		
		User userLogin = userService.login(user);
		if(userLogin != null){
			session.put("user_Login", userLogin);
			return SUCCESS;
		}else{
			msg = "用户名或密码错误！";
		}
		return "fail";
		
	}
	public String register(){
		user.setPower(1);
		User userRegister = userService.register(user);
		if(userRegister == null){
			msg = "账户已存在！";
			return "exist";
			
		}
		return SUCCESS;
	}
	
	public String out(){
		session.remove("user_Login");
		return "out";
	}
	
	@SuppressWarnings("deprecation")
	public String updataPassWord(){
		if(userService.updataPassWord(user, userDTO)){
			inputStream = new StringBufferInputStream("updateSuccess");
		} else {
			inputStream = new StringBufferInputStream("updateFailed");
		}
		return "updataSuccess";
	};

	public UserServiceInterface getUserService() {
		return userService;
	}

	public void setUserService(UserServiceInterface userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Map<String, Object> getSession() {
		return session;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	public UserDTO getUserDTO() {
		return userDTO;
	}
	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
	public String getUpdataMsg() {
		return updataMsg;
	}
	public void setUpdataMsg(String updataMsg) {
		this.updataMsg = updataMsg;
	}
	
	
	

}
